package com.fengze.business.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.fengze.business.service.IInquiryRecordService;
import com.fengze.business.vo.EarlyStageProjectVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fengze.common.annotation.Log;
import com.fengze.common.core.controller.BaseController;
import com.fengze.common.core.domain.AjaxResult;
import com.fengze.common.enums.BusinessType;
import com.fengze.business.domain.EarlyStageProject;
import com.fengze.business.service.IEarlyStageProjectService;
import com.fengze.common.utils.poi.ExcelUtil;
import com.fengze.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目前期询价项目Controller
 * 
 * @author lug
 * @date 2024-02-01
 */
@RestController
@RequestMapping("/preproject/info")
public class EarlyStageProjectController extends BaseController
{
    @Resource
    private IEarlyStageProjectService earlyStageProjectService;

    @Resource
    private IInquiryRecordService inquiryRecordService;

    /**
     * 查询项目前期询价项目列表
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(EarlyStageProjectVO earlyStageProjectVO)
    {
        startPage();
        List<EarlyStageProjectVO> list = earlyStageProjectService.selectEarlyStageProjectVOList(earlyStageProjectVO);
        TableDataInfo dataTable = getDataTable(list);
        for (Object row : dataTable.getRows()) {
            EarlyStageProjectVO vo = (EarlyStageProjectVO) row;

            // 获取询价数量
            int count = inquiryRecordService.countByStageProjectId(vo.getId());
            vo.setInquiryCount(count);
        }
        return dataTable;
    }

    /**
     * 导出项目前期询价项目列表
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:export')")
    @Log(title = "项目前期询价项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EarlyStageProject earlyStageProject)
    {
        List<EarlyStageProject> list = earlyStageProjectService.selectEarlyStageProjectList(earlyStageProject);
        ExcelUtil<EarlyStageProject> util = new ExcelUtil<EarlyStageProject>(EarlyStageProject.class);
        util.exportExcel(response, list, "项目前期询价项目数据");
    }

    /**
     * 获取项目前期询价项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(earlyStageProjectService.selectEarlyStageProjectVOById(id));
    }

    /**
     * 新增项目前期询价项目
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:add')")
    @Log(title = "项目前期询价项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(EarlyStageProject earlyStageProject, MultipartFile file)
    {
        return toAjax(earlyStageProjectService.insertEarlyStageProjectWithFile(earlyStageProject,file));
    }

    /**
     * 修改项目前期询价项目
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:edit')")
    @Log(title = "项目前期询价项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit( EarlyStageProject earlyStageProject,MultipartFile file)
    {
        return toAjax(earlyStageProjectService.updateEarlyStageProjectWithFile(earlyStageProject,file));
    }

    /**
     * 删除项目前期询价项目
     */
    @PreAuthorize("@ss.hasPermi('preproject:info:remove')")
    @Log(title = "项目前期询价项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(earlyStageProjectService.deleteEarlyStageProjectByIds(ids));
    }
}
